﻿using UnityEngine;
using System.Collections;
using System;

public class sdUIPetTujianCard : MonoBehaviour 
{
	public int m_iPetID = -1;
	public int index = -1;
	public bool m_bSelect	= false;	//是否选中..
	public bool m_bGray = false;

	public GameObject bg = null;
	public GameObject bgColor = null;
	public GameObject icon = null;
	public GameObject txtLevel = null;
	public GameObject type = null;
	public GameObject bz = null;
	public GameObject plock = null;
	public GameObject select = null;
	public GameObject star0 = null;//星星标志0..
	public GameObject star1	= null;//星星标志1..
	public GameObject star2	= null;//星星标志2..
	public GameObject star3	= null;//星星标志3..
	public GameObject star4	= null;//星星标志4..
	public GameObject txtName = null;//宠物名字..
	public GameObject up = null;//强化等级..
	
	void Awake () 
	{
	}
	
	void Start () 
	{
	}
	
	void Update () 
	{
	}
	
	void OnClick()
	{
		if (gameObject)
		{
			if( m_iPetID>0 )
				sdUIPetControl.Instance.ActivePetSmallTip(null, m_iPetID, 0, 1);
		}
	}
	
	public void ReflashPetIconUI(int iID)
	{
		if (iID==-1)
		{
			m_iPetID = -1;
			gameObject.SetActive(false);
			m_bSelect	= false;
			return;
		}
		else if (iID==0)
		{
			m_iPetID = 0;
			gameObject.SetActive(true);
			m_bSelect	= false;

			if (bg)
				bg.SetActive(true);

			if (bgColor)
				bgColor.SetActive(false);

			return;
		}

		m_iPetID = iID;
		gameObject.SetActive(true);

		if (bg)
			bg.SetActive(false);

		if (bgColor)
			bgColor.SetActive(true);

		Hashtable Info = sdConfDataMgr.Instance().GetPetTemplate(m_iPetID.ToString());
		if (Info == null)
			return;

		if (icon)
			icon.GetComponent<UISprite>().spriteName = Info["Icon"].ToString();

		if (txtLevel)
			txtLevel.GetComponent<UILabel>().text = "1";

		if (type)
		{
			int iType = int.Parse(Info["BaseJob"].ToString());
			if (iType==1)
			{
				type.GetComponent<UISprite>().spriteName = "IPzs";
				type.SetActive(true);
			}
			else if (iType==2)
			{
				type.GetComponent<UISprite>().spriteName = "IPfs";
				type.SetActive(true);
			}
			else if (iType==3)
			{
				type.GetComponent<UISprite>().spriteName = "IPyx";
				type.SetActive(true);
			}
			else if (iType==4)
			{
				type.GetComponent<UISprite>().spriteName = "IPms";
				type.SetActive(true);
			}
			else
			{
				type.SetActive(false);
			}
		}
		
		if (bz)
			bz.SetActive(false);
		
		if (plock)
			plock.SetActive(false);

		ReflashSelectUI();
		ReflashGrayUI();
		int iAbility = int.Parse(Info["Ability"].ToString());
		SetPetStar(iAbility);
		
		if (txtName)
		{
			sdNewPetMgr.SetLabelColorByAbility(iAbility, txtName);
			txtName.GetComponent<UILabel>().text = Info["Name"].ToString();
		}
		
		if (up)
		{
			up.GetComponent<UISprite>().spriteName = "pet_a1";
			up.SetActive(false);
		}
	}
	
	public void SetPetStar(int iStar)
	{
		//底板颜色..
		if (bgColor)
		{
			string strPicName = string.Format("petclass{0}", iStar);
			bgColor.GetComponent<UISprite>().spriteName = strPicName;
			bgColor.SetActive(true);
		}

		//星级..
		if (star0==null || star1==null || star2==null || star3==null || star4==null)
			return;
		
		float fWidth = (float)star0.GetComponent<UISprite>().width*1.0f;
		
		if (iStar==1)
		{
			star0.SetActive(false);
			star1.SetActive(false);
			star2.SetActive(true);
			star3.SetActive(false);
			star4.SetActive(false); 
			
			star2.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*2.0f, star2.GetComponent<UISprite>().transform.localPosition.y, 
					star2.GetComponent<UISprite>().transform.localPosition.z);
		}
		else if (iStar==2)
		{
			star0.SetActive(false);
			star1.SetActive(false);
			star2.SetActive(true);
			star3.SetActive(true);
			star4.SetActive(false);
			
			star2.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*1.5f, star2.GetComponent<UISprite>().transform.localPosition.y, 
					star2.GetComponent<UISprite>().transform.localPosition.z);
			star3.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*2.5f, star3.GetComponent<UISprite>().transform.localPosition.y, 
					star3.GetComponent<UISprite>().transform.localPosition.z);
		}
		else if (iStar==3)
		{
			star0.SetActive(false);
			star1.SetActive(true);
			star2.SetActive(true);
			star3.SetActive(true);
			star4.SetActive(false);
			
			star1.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*1.0f, star1.GetComponent<UISprite>().transform.localPosition.y, 
					star1.GetComponent<UISprite>().transform.localPosition.z);
			star2.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*2.0f, star2.GetComponent<UISprite>().transform.localPosition.y, 
					star2.GetComponent<UISprite>().transform.localPosition.z);
			star3.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*3.0f, star3.GetComponent<UISprite>().transform.localPosition.y, 
					star3.GetComponent<UISprite>().transform.localPosition.z);
		}
		else if (iStar==4)
		{
			star0.SetActive(false);
			star1.SetActive(true);
			star2.SetActive(true);
			star3.SetActive(true);
			star4.SetActive(true);
			
			star1.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*0.5f, star1.GetComponent<UISprite>().transform.localPosition.y, 
					star1.GetComponent<UISprite>().transform.localPosition.z);
			star2.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*1.5f, star2.GetComponent<UISprite>().transform.localPosition.y, 
					star2.GetComponent<UISprite>().transform.localPosition.z);
			star3.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*2.5f, star3.GetComponent<UISprite>().transform.localPosition.y, 
					star3.GetComponent<UISprite>().transform.localPosition.z);
			star4.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*3.5f, star4.GetComponent<UISprite>().transform.localPosition.y, 
					star4.GetComponent<UISprite>().transform.localPosition.z);
		}
		else if (iStar==5)
		{
			star0.SetActive(true);
			star1.SetActive(true);
			star2.SetActive(true);
			star3.SetActive(true);
			star4.SetActive(true);
			
			star1.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*1.0f, star1.GetComponent<UISprite>().transform.localPosition.y, 
					star1.GetComponent<UISprite>().transform.localPosition.z);
			star2.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*2.0f, star2.GetComponent<UISprite>().transform.localPosition.y, 
					star2.GetComponent<UISprite>().transform.localPosition.z);
			star3.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*3.0f, star3.GetComponent<UISprite>().transform.localPosition.y, 
					star3.GetComponent<UISprite>().transform.localPosition.z);
			star4.GetComponent<UISprite>().transform.localPosition = new Vector3
				(star0.GetComponent<UISprite>().transform.localPosition.x + fWidth*4.0f, star4.GetComponent<UISprite>().transform.localPosition.y, 
					star4.GetComponent<UISprite>().transform.localPosition.z);
		}
		else
		{
			star0.SetActive(true);
			star1.SetActive(false);
			star2.SetActive(false);
			star3.SetActive(false);
			star4.SetActive(false);
		}
	}

	public void SetSelect(bool bValue)
	{
		m_bSelect = bValue;
		ReflashSelectUI();
	}
	
	public void ReflashSelectUI()
	{
		if (select)
			select.SetActive(m_bSelect);
	}

	public void SetGray(bool bValue)
	{
		m_bGray = bValue;
		ReflashGrayUI();
	}
	
	public void ReflashGrayUI()
	{
		if (m_bGray)
		{
			Color grayColor = new Color (0.3f, 0.3f, 0.3f, 1f);

			if (bg)
				bg.GetComponent<UISprite>().color = grayColor;

			if (bgColor)
				bgColor.GetComponent<UISprite>().color = grayColor;

			if (icon)
				icon.GetComponent<UISprite>().color = grayColor;

			if (type)
				type.GetComponent<UISprite>().color = grayColor;

			if (bz)
				bz.GetComponent<UISprite>().color = grayColor;

			if (plock)
				plock.GetComponent<UISprite>().color = grayColor;

			if (select)
				select.GetComponent<UISprite>().color = grayColor;

			if (star0)
				star0.GetComponent<UISprite>().color = grayColor;

			if (star1)
				star1.GetComponent<UISprite>().color = grayColor;

			if (star2)
				star2.GetComponent<UISprite>().color = grayColor;

			if (star3)
				star3.GetComponent<UISprite>().color = grayColor;

			if (star4)
				star4.GetComponent<UISprite>().color = grayColor;

			if (up)
				up.GetComponent<UISprite>().color = grayColor;
		}
		else
		{
			if (bg)
				bg.GetComponent<UISprite>().color = Color.white;
			
			if (bgColor)
				bgColor.GetComponent<UISprite>().color = Color.white;
			
			if (icon)
				icon.GetComponent<UISprite>().color = Color.white;
			
			if (type)
				type.GetComponent<UISprite>().color = Color.white;
			
			if (bz)
				bz.GetComponent<UISprite>().color = Color.white;
			
			if (plock)
				plock.GetComponent<UISprite>().color = Color.white;
			
			if (select)
				select.GetComponent<UISprite>().color = Color.white;
			
			if (star0)
				star0.GetComponent<UISprite>().color = Color.white;
			
			if (star1)
				star1.GetComponent<UISprite>().color = Color.white;
			
			if (star2)
				star2.GetComponent<UISprite>().color = Color.white;
			
			if (star3)
				star3.GetComponent<UISprite>().color = Color.white;
			
			if (star4)
				star4.GetComponent<UISprite>().color = Color.white;
			
			if (up)
				up.GetComponent<UISprite>().color = Color.white;
		}
	}
}